/*-------------------------------------
		sPlugin
	    By Scooby, 2010
-------------------------------------*/

local PLUGIN = {}

PLUGIN.Name = "Spectate"
PLUGIN.Author = "Scooby"
PLUGIN.Command = "spectate"
PLUGIN.Group = 2
PLUGIN.Category = "Administration"
PLUGIN.Format = "string"
PLUGIN.Use = "<Player> 0 To Disable"
PLUGIN.Menu = {
	{
		title = "Choose Player",
		cont = "#players",
		apos = 1
	}
}


function PLUGIN:Main( ply, args )
	
	if( tonumber( args[1] ) == 0 ) then
			hook.Remove( "PlayerLoadout", "Admin_Spec"..ply:SteamID() )
			hook.Remove( "DoPlayerDeath", "Admin_Spec_NoDie"..ply:SteamID() )
			ply:Spectate( OBS_MODE_NONE )
			ply:UnSpectate()
			ply:SetMoveType( MOVETYPE_WALK )
			ply:Spawn()
			ply:SetPos( ply:GetNWVector( "ADMIN_SPECPOS" ) )
			spl:PrintC( ply, spl.blue, "You Are No Longer Spectating" )
	else
	
	local pl = spl:FindPly( args[1] )
	
	if( #pl == 0 ) then
		spl:PrintC( ply, spl.red, spl.fnp )
		return false
	elseif( #pl > 1 ) then
		spl:PrintC( ply, spl.red, spl.fmp )
		return false
	end
	local temp = {}
	for _,v in ipairs( ply:GetWeapons() ) do
		table.insert( temp, v:GetClass() )
	end
	
	ply:SetNWVector( "ADMIN_SPECPOS", ply:GetPos() )
	
		ply:Spectate( OBS_MODE_CHASE )
		ply:SpectateEntity( pl[1] )
		ply:SetMoveType( MOVETYPE_OBSERVER )
		ply:StripWeapons()
	
		hook.Add( "PlayerLoadout", "Admin_Spec"..ply:SteamID(), function( plys )
			if( ply == plys ) then
				plys:Spectate( OBS_MODE_CHASE )
				plys:SpectateEntity( pl[1] )
				plys:SetMoveType( MOVETYPE_OBSERVER )
				plys:StripWeapons()
				return false
			end
		end)
		
		hook.Add( "DoPlayerDeath", "Admin_Spec_NoDie"..ply:SteamID(), function( plys )
			if( ply == plys ) then
				return false
			end
		end)
	
	spl:PrintC( ply, spl.white, "You Are Now Spectating ", spl.green, pl[1]:Nick() )
	
end

end

spl:AddPlugin( PLUGIN )